package com.sdh.onlinemarket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdh.onlinemarket.entity.Item;
import com.sdh.onlinemarket.entity.Shopcard;
import com.sdh.onlinemarket.mapper.ShopcardMapper;
import com.sdh.onlinemarket.service.ItemService;
import com.sdh.onlinemarket.service.ShopcardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 施德昊
 * @since 2021-11-21
 */
@Service
public class ShopcardServiceImpl extends ServiceImpl<ShopcardMapper, Shopcard> implements ShopcardService {

    @Autowired
    ShopcardService shopcardService;
    @Autowired
    ItemService itemService;

    public boolean updateStorage(String userid){
        List<Shopcard> shopcardList = shopcardService.list(new QueryWrapper<Shopcard>().eq("userid",userid));
        for (Shopcard shopcardt:shopcardList){
            Item item = itemService.getOne(new QueryWrapper<Item>().eq("itemid",shopcardt.getItemid()));
            item.setStorage(item.getStorage()-shopcardt.getInum());
            if (item.getStorage()<0){
                return false;
            }
            itemService.update(item,new QueryWrapper<Item>().eq("itemid",item.getItemid()));
        }
        return true;
    }

}
